<!doctype html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../assert_selection.js"></script>
<script>
// Tests to ensure MakeTextWritingDirection* modifies the direction of text and
// embed texts properly.
function editable(sample) {
  return `<div contenteditable>${sample}</div>`;
}

function testIt(input, command_name, expected, description) {
  selection_test(
      editable(input),
      (selection, testRunner) => {
        testRunner.execCommand(command_name);
      },
      editable(expected),
      description);
}

// left to right language
testIt('^abc| xyz',
       'MakeTextWritingDirectionNatural',
       '^abc| xyz',
       'ltr-01 natural first word');
testIt('^abc| xyz',
       'MakeTextWritingDirectionLeftToRight',
       '<span style="unicode-bidi: isolate;">^abc|</span> xyz',
       'ltr-02 ltr first word');
testIt('^abc| xyz',
       'MakeTextWritingDirectionRightToLeft',
       '<span style="unicode-bidi: isolate; direction: rtl;">^abc|</span> xyz',
       'ltr-03 rtl first word');
testIt('<b>abc ^xyz</b> wxy|',
       'MakeTextWritingDirectionNatural',
       '<b>abc ^xyz</b> wxy|',
       'ltr-03 natural second and third words');
testIt('<b>abc ^xyz</b> wxy|',
       'MakeTextWritingDirectionLeftToRight',
       '<b>abc <span style="unicode-bidi: isolate;">^xyz</span></b><span style="unicode-bidi: isolate;"> wxy|</span>',
       'ltr-04 ltr second and third words');
testIt('<b>abc ^xyz</b> wxy|',
       'MakeTextWritingDirectionRightToLeft',
       '<b>abc <span style="unicode-bidi: isolate; direction: rtl;">^xyz</span></b><span style="unicode-bidi: isolate; direction: rtl;"> wxy|</span>',
       'ltr-05  second and third words');
testIt('<span dir="rtl">abc <span dir="ltr">xyz ^wxy| rst</span></span>',
       'MakeTextWritingDirectionNatural',
       '<span dir="rtl">abc <span dir="ltr">xyz </span></span><span><span>^wxy|</span></span><span dir="rtl"><span dir="ltr"> rst</span></span>',
       'ltr-06 third word');
testIt('<span dir="rtl">abc <span dir="ltr">xyz ^wxy| rst</span></span>',
       'MakeTextWritingDirectionLeftToRight',
       '<span dir="rtl">abc <span dir="ltr">xyz </span></span><span><span style="unicode-bidi: isolate;">^wxy|</span></span><span dir="rtl"><span dir="ltr"> rst</span></span>',
       'ltr-07 third word');
testIt('<span dir="rtl">abc <span dir="ltr">xyz ^wxy| rst</span></span>',
       'MakeTextWritingDirectionRightToLeft',
       '<span dir="rtl">abc <span dir="ltr">xyz </span><span>^wxy|</span><span dir="ltr"> rst</span></span>',
       'ltr-08 third word');

// right to left language
const kRtl0 = '\u0647\u0646\u0627';
const kRtl1 = '\u064A\u0643\u062A\u0628';
const kRtl2 = '\u0627\u0644\u0646\u0635';
const kRtl3 = '\u0627\u0644\u0639\u0631\u0628\u064a';

testIt(`^${kRtl0}| ${kRtl1} ${kRtl2} ${kRtl3}`,
       'MakeTextWritingDirectionNatural',
       `^${kRtl0}| ${kRtl1} ${kRtl2} ${kRtl3}`,
       'rtl-01 natural first word');
testIt(`^${kRtl0}| ${kRtl1} ${kRtl2} ${kRtl3}`,
       'MakeTextWritingDirectionLeftToRight',
       `<span style="unicode-bidi: isolate;">^${kRtl0}|</span> ${kRtl1} ${kRtl2} ${kRtl3}`,
       'rtl-02 ltr first word');
testIt(`^${kRtl0}| ${kRtl1} ${kRtl2} ${kRtl3}`,
       'MakeTextWritingDirectionRightToLeft',
       `<span style="unicode-bidi: isolate; direction: rtl;">^${kRtl0}|</span> ${kRtl1} ${kRtl2} ${kRtl3}`,
       'rtl-03 rtl first word');

testIt(`<b>${kRtl0} ^${kRtl1}</b> ${kRtl2}| ${kRtl3}`,
       'MakeTextWritingDirectionNatural',
       `<b>${kRtl0} ^${kRtl1}</b> ${kRtl2}| ${kRtl3}`,
       'rtl-04 natural second and third words');
testIt(`<b>${kRtl0} ^${kRtl1}</b> ${kRtl2}| ${kRtl3}`,
       'MakeTextWritingDirectionLeftToRight',
       `<b>${kRtl0} <span style="unicode-bidi: isolate;">^${kRtl1}</span></b><span style="unicode-bidi: isolate;"> ${kRtl2}|</span> ${kRtl3}`,
       'rtl-05 ltr second and third words');
testIt(`<b>${kRtl0} ^${kRtl1}</b> ${kRtl2}| ${kRtl3}`,
       'MakeTextWritingDirectionRightToLeft',
       `<b>${kRtl0} <span style="unicode-bidi: isolate; direction: rtl;">^${kRtl1}</span></b><span style="unicode-bidi: isolate; direction: rtl;"> ${kRtl2}|</span> ${kRtl3}`,
       'rtl-06  rtl second and third words');

testIt(`<div dir="rtl">^${kRtl0}| ${kRtl1} ${kRtl2} ${kRtl3}</div>`,
       'MakeTextWritingDirectionNatural',
       `<div dir="rtl">^${kRtl0}| ${kRtl1} ${kRtl2} ${kRtl3}</div>`,
       'rtl-07 natural first word');
testIt(`<div dir="rtl">^${kRtl0}| ${kRtl1} ${kRtl2} ${kRtl3}</div>`,
       'MakeTextWritingDirectionLeftToRight',
       `<div dir="rtl"><span style="unicode-bidi: isolate; direction: ltr;">^${kRtl0}|</span> ${kRtl1} ${kRtl2} ${kRtl3}</div>`,
       'rtl-08 ltr first word');
testIt(`<div dir="rtl">^${kRtl0}| ${kRtl1} ${kRtl2} ${kRtl3}</div>`,
       'MakeTextWritingDirectionRightToLeft',
       `<div dir="rtl"><span style="unicode-bidi: isolate;">^${kRtl0}|</span> ${kRtl1} ${kRtl2} ${kRtl3}</div>`,
       'rtl-09 rtl first wrod');

testIt(`<div dir="rtl"><b>${kRtl0} ^${kRtl1}</b> ${kRtl2}| ${kRtl3}</div>`,
       'MakeTextWritingDirectionNatural',
       `<div dir="rtl"><b>${kRtl0} ^${kRtl1}</b> ${kRtl2}| ${kRtl3}</div>`,
       'rtl-10 natural second and third words');
testIt(`<div dir="rtl"><b>${kRtl0} ^${kRtl1}</b> ${kRtl2}| ${kRtl3}</div>`,
       'MakeTextWritingDirectionLeftToRight',
       `<div dir="rtl"><b>${kRtl0} <span style="unicode-bidi: isolate; direction: ltr;">^${kRtl1}</span></b><span style="unicode-bidi: isolate; direction: ltr;"> ${kRtl2}|</span> ${kRtl3}</div>`,
       'rtl-11 ltr second and third words');
testIt(`<div dir="rtl"><b>${kRtl0} ^${kRtl1}</b> ${kRtl2}| ${kRtl3}</div>`,
       'MakeTextWritingDirectionRightToLeft',
       `<div dir="rtl"><b>${kRtl0} <span style="unicode-bidi: isolate;">^${kRtl1}</span></b><span style="unicode-bidi: isolate;"> ${kRtl2}|</span> ${kRtl3}</div>`,
       'rtl-12 rtl second third words');

testIt(`<div dir="rtl">${kRtl0} <span dir="ltr">${kRtl1} ^${kRtl2}| ${kRtl3}</span></div>`,
       'MakeTextWritingDirectionNatural',
       `<div dir="rtl">${kRtl0} <span dir="ltr">${kRtl1} </span><span>^${kRtl2}|</span><span dir="ltr"> ${kRtl3}</span></div>`,
       'rtl-13 natural third word');
testIt(`<div dir="rtl">${kRtl0} <span dir="ltr">${kRtl1} ^${kRtl2}| ${kRtl3}</span></div>`,
       'MakeTextWritingDirectionLeftToRight',
       `<div dir="rtl"><span style="unicode-bidi: isolate; direction: ltr;">${kRtl0} ${kRtl1} ^${kRtl2}| ${kRtl3}</span></div>`,
       'rtl-14 ltr third word');
testIt(`<div dir="rtl">${kRtl0} <span dir="ltr">${kRtl1} ^${kRtl2}| ${kRtl3}</span></div>`,
       'MakeTextWritingDirectionRightToLeft',
       `<div dir="rtl">${kRtl0} <span dir="ltr">${kRtl1} </span><span style="unicode-bidi: isolate;">^${kRtl2}|</span><span dir="ltr"> ${kRtl3}</span></div>`,
       'rtl-15 rtl third word');

// bidirectional langauge
const kCjk0 = '\u5199';
const kCjk1 = '\u4e2d\u6587';

testIt(`^${kCjk0}|${kCjk1}`,
       'MakeTextWritingDirectionNatural',
       `^${kCjk0}|${kCjk1}`,
       'cjk-ltr-1 natural');
testIt(`^${kCjk0}|${kCjk1}`,
       'MakeTextWritingDirectionLeftToRight',
       `<span style="unicode-bidi: isolate;">^${kCjk0}|</span>${kCjk1}`,
       'cjk-ltr-2 ltr');
testIt(`^${kCjk0}|${kCjk1}`,
       'MakeTextWritingDirectionRightToLeft',
       `<span style="unicode-bidi: isolate; direction: rtl;">^${kCjk0}|</span>${kCjk1}`,
       'cjk-ltr-3 rtl');

testIt(`<div dir="rtl">^${kCjk0}|${kCjk1}</div>`,
       'MakeTextWritingDirectionNatural',
       `<div dir="rtl">^${kCjk0}|${kCjk1}</div>`,
       'cjk-rtl-1 natural');
testIt(`<div dir="rtl">^${kCjk0}|${kCjk1}</div>`,
       'MakeTextWritingDirectionLeftToRight',
       `<div dir="rtl"><span style="unicode-bidi: isolate; direction: ltr;">^${kCjk0}|</span>${kCjk1}</div>`,
       'cjk-rtl-2 ltr');
testIt(`<div dir="rtl">^${kCjk0}|${kCjk1}</div>`,
       'MakeTextWritingDirectionRightToLeft',
       `<div dir="rtl"><span style="unicode-bidi: isolate;">^${kCjk0}|</span>${kCjk1}</div>`,
       'cjk-rtl-3 rtl');
</script>
